package com.colin.controller;

import com.colin.pojo.Users;
import com.colin.service.ex.ServiceException;
import com.colin.service.impl.UsersServiceImpl;
import com.colin.utils.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

public class BaseController{
    protected final static Integer OK = 200;
    @Autowired
    private UsersServiceImpl usersService;

    @ExceptionHandler(ServiceException.class)
    public JsonResult<Void> exceptionHandler(Throwable e){
        JsonResult<Void> result = new JsonResult<Void>(e);
        result.setMessage(e.getMessage());
        result.setStatus(400);
        return result;
    }

    // 获取用户ID
    protected int getUid(){
        Object id = SecurityUtils.getSubject().getSession().getAttribute("userInfoId");
        if (id == null){
            return 0;
        }
        return (int) id;
    }

    // 获取用户信息
    protected Users getUserInfo(){
        int uid = getUid();
        return usersService.queryUserInfo(uid);
    }
}
